<?php
declare (strict_types=1);

namespace app\store\controller\questionnaire;

use app\store\controller\Controller;
use app\store\model\questionnaire\Topic as Model;
use think\db\exception\DbException;

class Topic extends Controller
{
    /**
     * 详情
     * @return array
     */
    public function details($apiId)
    {
        $details = Model::detail($apiId);
        return $this->renderSuccess(compact('details'));
    }

    /**
     * 列表
     * @return array
     * @throws DbException
     */
    public function list()
    {
        $model = new Model;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    public function questionnaireList($questionnaireId)
    {
        $model = new Model;
        $list = $model->questionnaireList($questionnaireId);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 删除
     * @param int $apiId
     * @return array|bool
     */
    public function delete(int $apiId)
    {
        // 管理员详情
        $model = Model::detail($apiId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 新增
     * @return array
     */
    public function saveList($questionnaire_id)
    {
        $model = new Model;
        // 更新记录
        if ($model->saveList($questionnaire_id, $this->postForm())) {
            return $this->renderSuccess('保存成功');
        }
        return $this->renderError($model->getError() ?: '保存失败');
    }

    /**
     * 新增
     * @return array
     */
    public function add()
    {
        $model = new Model;
        // 更新记录
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新
     * @param int $apiId
     * @return array
     */
    public function edit(int $apiId)
    {
        // 管理员详情
        $model = Model::detail($apiId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
}
